<template>
    <div class="role">
        <div class="btns">
            <el-button @click="roleFormVisible = true" size="small" type="primary" icon="el-icon-plus">新增</el-button>
        </div>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="角色编号" width="180">
            </el-table-column>
            <el-table-column prop="name" label="角色名称" width="180">
            </el-table-column>
            <!-- 权限字符 -->
            <el-table-column prop="permissionList" label="权限字符" width="180">
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                    <el-switch active-color="#13ce66" inactive-color="#1890FF">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="createdAt" label="创建时间">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="添加角色" :visible.sync="roleFormVisible">
            <el-form :rules="rules" ref="form" :model="form">
                <el-form-item label="角色名称" :label-width="formLabelWidth">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="权限字符" :label-width="formLabelWidth">
                    <el-input v-model="form.permissionList" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-radio-group v-model="form.resource">
                        <el-radio label="正常"></el-radio>
                        <el-radio label="停用"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item class="btn">
                    <el-button @click="roleFormVisible = false">取 消</el-button>
                    <el-button :disabled="submitting" type="primary" @click="submitForm('form')">确 定</el-button>
                </el-form-item>
            </el-form>
            <!-- <div slot="footer" class="dialog-footer">
            </div> -->
        </el-dialog>

        <el-dialog title="修改角色" :visible.sync="form2Visible">
            <el-form :rules="rules" ref="formEdit" :model="formEdit">
                <el-form-item label="角色名称" :label-width="formLabelWidth">
                    <el-input v-model="formEdit.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="权限字符" :label-width="formLabelWidth">
                    <el-input v-model="formEdit.permissionList" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-radio-group v-model="formEdit.resource">
                        <el-radio label="正常"></el-radio>
                        <el-radio label="停用"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item class="btn">
                    <el-button @click="form2Visible = false">取 消</el-button>
                    <el-button :disabled="submitting2" type="primary" @click="submitEditForm('formEdit')">确 定</el-button>
                </el-form-item>
            </el-form>
            <!-- <div slot="footer" class="dialog-footer">
            </div> -->
        </el-dialog>
    </div>
</template>

<script>
import { addRole, listRole, delRole, editRole } from '@/api/authority';


export default {
    data() {
        return {
            // 添加角色表单可见性
            roleFormVisible: false,
            // 修改角色表单可见性
            form2Visible: false,
            submitting: false,
            submitting2: false,
            tableData: [],
            // [{
            //     id: '1',
            //     name: '超级管理员',
            //     character: "	admin",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }, {
            //     id: '1',
            //     name: '超级管理员',
            //     character: "	admin",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }, {
            //     id: '1',
            //     name: '超级管理员',
            //     character: "	admin",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }, {
            //     id: '1',
            //     name: '超级管理员',
            //     character: "	admin",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }, {
            //     id: '1',
            //     name: '超级管理员',
            //     character: "	admin",
            //     status: 1,
            //     time: "2023-07-04 10:44:46"
            // }],
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: ''
            },
            formEdit: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: ''
            },
            formLabelWidth: '120px',
            //权限信息对象
            authInfo: {},
            rules:{

            }
        }
    },
    mounted() {
        this.getRoleList();
    },
    methods: {
        //查看角色信息表
        getRoleList() {
            listRole().then(res => {
                console.log("角色信息表", res)
                const data = res.data;
                this.tableData = data;
            })
        },
        submitForm(formName) {
            // console.log(JSON.stringify(this.attachArr))
            // return;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submitting = true;
                    addRole(this.form).then(res => {
                        if (res.code == 200) {
                            this.$message({
                                message: '角色新增成功',
                                type: 'success'
                            });
                            setTimeout(() => {
                                this.roleFormVisible = false,
                                this.$router.push("/role")//返回的网页路径
                            })
                            this.submitting = false;
                        }
                    })
                } else {
                    return false;
                }
            })
        },
        submitEditForm(formName) {
            // console.log(JSON.stringify(this.attachArr))
            // return;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submitting2 = true;
                    editRole(this.formEdit).then(res => {
                        if (res.code == 200) {
                            this.$message({
                                message: '角色修改成功',
                                type: 'success'
                            });
                            setTimeout(() => {
                                this.form2Visible = false,
                                this.$router.push("/role")//返回的网页路径
                            })
                            this.submitting2 = false;
                        }
                    })
                } else {
                    return false;
                }
            })
        },
        //修改
        handleEdit(index, row){
            //显示修改表单
            this.form2Visible = true
            this.formEdit = row
        },
        //删除
        handleDelete(index, row) {
            console.log("删除角色：参数index", index)
            console.log("删除角色：参数row", row)
            this.$confirm('确定删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delRole(row.id).then(res => {
                    if (res.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getRoleList()
                    }
                })
            }).catch(() => { });
        }
    }

}
</script>

<style lang="scss" scoped>
.role {
    padding: 20px;

    .btns {
        margin-top: 35px;
    }

    .el-table {
        margin-top: 20px;
    }
    .btn {
    display: flex;
    justify-content: flex-end;
}
}
</style>